{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 12,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "person         Wes Mckinney\n",
      "who       Creator of Pandas\n",
      "dtype: object\n"
     ]
    }
   ],
   "source": [
    "import pandas as pd\n",
    "END_STRING = '\\n'+'--------------------------------'+'\\n' #规定print函数的end参数格式，使其显示更为清晰\n",
    "## 创建数据\n",
    "s = pd.Series(['Wes Mckinney','Creator of Pandas'],index = ['person','who']) \n",
    "print(s)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "The name is: Alice\n",
      "The name is: 123\n",
      "The name is: (1, 2, 3)\n"
     ]
    }
   ],
   "source": [
    "from typing import Hashable\n",
    "\n",
    "def process_name(name: Hashable=...):\n",
    "    print(f\"The name is: {name}\")\n",
    "\n",
    "# 调用函数\n",
    "process_name(\"Alice\")\n",
    "process_name(123)\n",
    "process_name((1, 2, 3))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Alice\n",
      "123\n",
      "(1, 2, 3)\n"
     ]
    }
   ],
   "source": [
    "class MyClass:\n",
    "    def __init__(self, name: Hashable = ...):\n",
    "        self.name = name\n",
    "\n",
    "# 创建对象\n",
    "obj1 = MyClass(\"Alice\") #Alice\n",
    "obj2 = MyClass(123) #123\n",
    "obj3 = MyClass((1, 2, 3)) #(1, 2, 3)\n",
    "\n",
    "print(obj1.name)\n",
    "print(obj2.name)\n",
    "print(obj3.name)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "metadata": {},
   "outputs": [
    {
     "ename": "ValueError",
     "evalue": "invalid literal for int() with base 10: 'I'",
     "output_type": "error",
     "traceback": [
      "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m",
      "\u001b[1;31mValueError\u001b[0m                                Traceback (most recent call last)",
      "Cell \u001b[1;32mIn[15], line 5\u001b[0m\n\u001b[0;32m      3\u001b[0m dtype \u001b[38;5;241m=\u001b[39m \u001b[38;5;124m'\u001b[39m\u001b[38;5;124mint64\u001b[39m\u001b[38;5;124m'\u001b[39m\n\u001b[0;32m      4\u001b[0m iscopy \u001b[38;5;241m=\u001b[39m \u001b[38;5;28;01mTrue\u001b[39;00m\n\u001b[1;32m----> 5\u001b[0m s_new \u001b[38;5;241m=\u001b[39m \u001b[43mpd\u001b[49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43mSeries\u001b[49m\u001b[43m(\u001b[49m\u001b[43mdata\u001b[49m\u001b[38;5;241;43m=\u001b[39;49m\u001b[43mdata\u001b[49m\u001b[43m,\u001b[49m\u001b[43mindex\u001b[49m\u001b[38;5;241;43m=\u001b[39;49m\u001b[43mindex\u001b[49m\u001b[43m,\u001b[49m\u001b[43mdtype\u001b[49m\u001b[38;5;241;43m=\u001b[39;49m\u001b[43mdtype\u001b[49m\u001b[43m,\u001b[49m\u001b[43mcopy\u001b[49m\u001b[38;5;241;43m=\u001b[39;49m\u001b[43miscopy\u001b[49m\u001b[43m)\u001b[49m\n\u001b[0;32m      6\u001b[0m \u001b[38;5;28mprint\u001b[39m(s_new)\n",
      "File \u001b[1;32mc:\\Users\\lenovo\\miniconda3\\envs\\env_py38\\lib\\site-packages\\pandas\\core\\series.py:439\u001b[0m, in \u001b[0;36mSeries.__init__\u001b[1;34m(self, data, index, dtype, name, copy, fastpath)\u001b[0m\n\u001b[0;32m    437\u001b[0m         data \u001b[38;5;241m=\u001b[39m data\u001b[38;5;241m.\u001b[39mcopy()\n\u001b[0;32m    438\u001b[0m \u001b[38;5;28;01melse\u001b[39;00m:\n\u001b[1;32m--> 439\u001b[0m     data \u001b[38;5;241m=\u001b[39m \u001b[43msanitize_array\u001b[49m\u001b[43m(\u001b[49m\u001b[43mdata\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[43mindex\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[43mdtype\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[43mcopy\u001b[49m\u001b[43m)\u001b[49m\n\u001b[0;32m    441\u001b[0m     manager \u001b[38;5;241m=\u001b[39m get_option(\u001b[38;5;124m\"\u001b[39m\u001b[38;5;124mmode.data_manager\u001b[39m\u001b[38;5;124m\"\u001b[39m)\n\u001b[0;32m    442\u001b[0m     \u001b[38;5;28;01mif\u001b[39;00m manager \u001b[38;5;241m==\u001b[39m \u001b[38;5;124m\"\u001b[39m\u001b[38;5;124mblock\u001b[39m\u001b[38;5;124m\"\u001b[39m:\n",
      "File \u001b[1;32mc:\\Users\\lenovo\\miniconda3\\envs\\env_py38\\lib\\site-packages\\pandas\\core\\construction.py:569\u001b[0m, in \u001b[0;36msanitize_array\u001b[1;34m(data, index, dtype, copy, raise_cast_failure, allow_2d)\u001b[0m\n\u001b[0;32m    566\u001b[0m     data \u001b[38;5;241m=\u001b[39m \u001b[38;5;28mlist\u001b[39m(data)\n\u001b[0;32m    568\u001b[0m \u001b[38;5;28;01mif\u001b[39;00m dtype \u001b[38;5;129;01mis\u001b[39;00m \u001b[38;5;129;01mnot\u001b[39;00m \u001b[38;5;28;01mNone\u001b[39;00m \u001b[38;5;129;01mor\u001b[39;00m \u001b[38;5;28mlen\u001b[39m(data) \u001b[38;5;241m==\u001b[39m \u001b[38;5;241m0\u001b[39m:\n\u001b[1;32m--> 569\u001b[0m     subarr \u001b[38;5;241m=\u001b[39m \u001b[43m_try_cast\u001b[49m\u001b[43m(\u001b[49m\u001b[43mdata\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[43mdtype\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[43mcopy\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[43mraise_cast_failure\u001b[49m\u001b[43m)\u001b[49m\n\u001b[0;32m    570\u001b[0m \u001b[38;5;28;01melse\u001b[39;00m:\n\u001b[0;32m    571\u001b[0m     subarr \u001b[38;5;241m=\u001b[39m maybe_convert_platform(data)\n",
      "File \u001b[1;32mc:\\Users\\lenovo\\miniconda3\\envs\\env_py38\\lib\\site-packages\\pandas\\core\\construction.py:754\u001b[0m, in \u001b[0;36m_try_cast\u001b[1;34m(arr, dtype, copy, raise_cast_failure)\u001b[0m\n\u001b[0;32m    748\u001b[0m \u001b[38;5;28;01mtry\u001b[39;00m:\n\u001b[0;32m    749\u001b[0m     \u001b[38;5;66;03m# GH#15832: Check if we are requesting a numeric dtype and\u001b[39;00m\n\u001b[0;32m    750\u001b[0m     \u001b[38;5;66;03m# that we can convert the data to the requested dtype.\u001b[39;00m\n\u001b[0;32m    751\u001b[0m     \u001b[38;5;28;01mif\u001b[39;00m is_integer_dtype(dtype):\n\u001b[0;32m    752\u001b[0m         \u001b[38;5;66;03m# this will raise if we have e.g. floats\u001b[39;00m\n\u001b[1;32m--> 754\u001b[0m         subarr \u001b[38;5;241m=\u001b[39m \u001b[43mmaybe_cast_to_integer_array\u001b[49m\u001b[43m(\u001b[49m\u001b[43marr\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[43mdtype\u001b[49m\u001b[43m)\u001b[49m\n\u001b[0;32m    755\u001b[0m     \u001b[38;5;28;01melse\u001b[39;00m:\n\u001b[0;32m    756\u001b[0m         \u001b[38;5;66;03m# 4 tests fail if we move this to a try/except/else; see\u001b[39;00m\n\u001b[0;32m    757\u001b[0m         \u001b[38;5;66;03m#  test_constructor_compound_dtypes, test_constructor_cast_failure\u001b[39;00m\n\u001b[0;32m    758\u001b[0m         \u001b[38;5;66;03m#  test_constructor_dict_cast2, test_loc_setitem_dtype\u001b[39;00m\n\u001b[0;32m    759\u001b[0m         subarr \u001b[38;5;241m=\u001b[39m np\u001b[38;5;241m.\u001b[39marray(arr, dtype\u001b[38;5;241m=\u001b[39mdtype, copy\u001b[38;5;241m=\u001b[39mcopy)\n",
      "File \u001b[1;32mc:\\Users\\lenovo\\miniconda3\\envs\\env_py38\\lib\\site-packages\\pandas\\core\\dtypes\\cast.py:2040\u001b[0m, in \u001b[0;36mmaybe_cast_to_integer_array\u001b[1;34m(arr, dtype, copy)\u001b[0m\n\u001b[0;32m   2038\u001b[0m \u001b[38;5;28;01mtry\u001b[39;00m:\n\u001b[0;32m   2039\u001b[0m     \u001b[38;5;28;01mif\u001b[39;00m \u001b[38;5;129;01mnot\u001b[39;00m \u001b[38;5;28misinstance\u001b[39m(arr, np\u001b[38;5;241m.\u001b[39mndarray):\n\u001b[1;32m-> 2040\u001b[0m         casted \u001b[38;5;241m=\u001b[39m \u001b[43mnp\u001b[49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43marray\u001b[49m\u001b[43m(\u001b[49m\u001b[43marr\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[43mdtype\u001b[49m\u001b[38;5;241;43m=\u001b[39;49m\u001b[43mdtype\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[43mcopy\u001b[49m\u001b[38;5;241;43m=\u001b[39;49m\u001b[43mcopy\u001b[49m\u001b[43m)\u001b[49m\n\u001b[0;32m   2041\u001b[0m     \u001b[38;5;28;01melse\u001b[39;00m:\n\u001b[0;32m   2042\u001b[0m         casted \u001b[38;5;241m=\u001b[39m arr\u001b[38;5;241m.\u001b[39mastype(dtype, copy\u001b[38;5;241m=\u001b[39mcopy)\n",
      "\u001b[1;31mValueError\u001b[0m: invalid literal for int() with base 10: 'I'"
     ]
    }
   ],
   "source": [
    "data = ['I','Love','Python','What about you']\n",
    "index = ['who','how','what','then']\n",
    "dtype = 'int64'\n",
    "iscopy = True\n",
    "s_new = pd.Series(data=data,index=index,dtype=dtype,copy=iscopy)\n",
    "print(s_new)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "0     I am\n",
      "1    Tuple\n",
      "dtype: object\n",
      "--------------------------------\n",
      "Key1    1\n",
      "Key2    2\n",
      "Key3    4\n",
      "dtype: object\n"
     ]
    }
   ],
   "source": [
    "s_tuple = pd.Series(('I am','Tuple')) #传入元组\n",
    "print(s_tuple,end=END_STRING)\n",
    "dirs = {'Key1':'1','Key2':'2','Key3':4}\n",
    "s_dirs = pd.Series(dirs)\n",
    "print(s_dirs)\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 创建Series\n",
    "1. 元素数据类型必须相同\n",
    "2. Series是一维容器，表示DataFrame的每一列\n",
    "3. 当传入的是混合类型的列表时，将会使用常见数据类型“Object”\n",
    "4. 可以将DataFrame看作是Series对象组成的字典：键代表列名，值是列的内容\n",
    "\n",
    "```python\n",
    "s = pd.Series(['banana',42])\n",
    "```"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "           Name  Age    Sex        Died\n",
      "ONE      fangao   62    man  1920-07-20\n",
      "TWO     dafenqi   73  woman  1990-09-25\n",
      "THREE  bijiasuo   84    man   1987-5-12\n"
     ]
    }
   ],
   "source": [
    "# 使用DataFrame创建数据\n",
    "'''\n",
    "我们注意到:使用pd.Series创建的数据有限，因此我们使用pd.DataFrame\n",
    "来构造多列数据。但使用字典创建数据时，由于字典中的顺序会发生改变，因而我们需要为其指定columns顺序。\n",
    "'''\n",
    "\n",
    "arts = pd.DataFrame({\n",
    "    'Name':['fangao','dafenqi','bijiasuo'],\n",
    "    'Age':[62,73,84],\n",
    "    'Sex':['man','woman','man'],\n",
    "    'Died':['1920-07-20','1990-09-25','1987-5-12']\n",
    "},index=['ONE','TWO','THREE'],\n",
    "columns=['Name','Age','Sex','Died'])\n",
    "print(arts)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "                   Name        Born        Died  Age          Occupation\n",
      "0     Rosaline Franklin  1920-07-25  1958-04-16   37             Chemist\n",
      "1        William Gosset  1876-06-13  1937-10-16   61        Statistician\n",
      "2  Florence Nightingale  1820-05-12  1910-08-13   90               Nurse\n",
      "3           Marie Curie  1867-11-07  1934-07-04   66             Chemist\n",
      "4         Rachel Carson  1907-05-27  1964-04-14   56           Biologist\n",
      "5             John Snow  1813-03-15  1858-06-16   45           Physician\n",
      "6           Alan Turing  1912-06-23  1954-06-07   41  Computer Scientist\n",
      "7          Johann Gauss  1777-04-30  1855-02-23   77       Mathematician\n"
     ]
    }
   ],
   "source": [
    "scientists = pd.read_csv('../data/scientists.csv')\n",
    "print(scientists)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "<class 'pandas.core.series.Series'>\n",
      "\n",
      "--------------------------------\n",
      " Name          Rosaline Franklin\n",
      "Born                 1920-07-25\n",
      "Died                 1958-04-16\n",
      "Age                          37\n",
      "Occupation              Chemist\n",
      "Name: 0, dtype: object\n",
      "\n",
      "--------------------------------\n",
      " ['Rosaline Franklin' '1920-07-25' '1958-04-16' 37 'Chemist']\n",
      "RangeIndex(start=0, stop=8, step=1)\n",
      "Name          object\n",
      "Born          object\n",
      "Died          object\n",
      "Age            int64\n",
      "Occupation    object\n",
      "dtype: object\n"
     ]
    }
   ],
   "source": [
    "# 获取切片\n",
    "william = scientists.iloc[0,:]\n",
    "print(type(william))\n",
    "print(END_STRING,william)\n",
    "print(END_STRING,william.values)\n",
    "print(scientists.index)\n",
    "print(scientists.dtypes)\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "0    37\n",
      "1    61\n",
      "2    90\n",
      "3    66\n",
      "4    56\n",
      "5    45\n",
      "6    41\n",
      "7    77\n",
      "Name: Age, dtype: int64 <class 'pandas.core.series.Series'> \n",
      "--------------------------------\n",
      "\n",
      "count     8.000000\n",
      "mean     59.125000\n",
      "std      18.325918\n",
      "min      37.000000\n",
      "25%      44.000000\n",
      "50%      58.500000\n",
      "75%      68.750000\n",
      "max      90.000000\n",
      "Name: Age, dtype: float64 \n",
      "--------------------------------\n",
      "\n",
      "<class 'pandas.core.series.Series'> Index(['count', 'mean', 'std', 'min', '25%', '50%', '75%', 'max'], dtype='object') 44.0 \n",
      "--------------------------------\n",
      "\n",
      "1    61\n",
      "2    90\n",
      "3    66\n",
      "7    77\n",
      "Name: Age, dtype: int64 \n",
      "--------------------------------\n",
      "\n"
     ]
    }
   ],
   "source": [
    "ages = scientists['Age']\n",
    "ages_describe = ages.describe()\n",
    "print(ages,type(ages),END_STRING)\n",
    "print(ages.describe(),END_STRING)\n",
    "print(type(ages_describe),ages_describe.index,\\\n",
    "      ages_describe['25%'],END_STRING)\n",
    "print(ages[ages >ages.mean()],END_STRING)\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "0     74\n",
      "1    122\n",
      "2    180\n",
      "3    132\n",
      "4    112\n",
      "5     90\n",
      "6     82\n",
      "7    154\n",
      "Name: Age, dtype: int64\n",
      "                   Name        Born        Died  Age     Occupation\n",
      "1        William Gosset  1876-06-13  1937-10-16   61   Statistician\n",
      "2  Florence Nightingale  1820-05-12  1910-08-13   90          Nurse\n",
      "3           Marie Curie  1867-11-07  1934-07-04   66        Chemist\n",
      "7          Johann Gauss  1777-04-30  1855-02-23   77  Mathematician\n"
     ]
    }
   ],
   "source": [
    "rev_ages = ages.sort_index(ascending = False)\n",
    "print(ages + rev_ages)\n",
    "print(scientists[scientists['Age'] > scientists['Age'].mean()])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 22,
   "metadata": {},
   "outputs": [
    {
     "ename": "ValueError",
     "evalue": "No axis named 1 for object type Series",
     "output_type": "error",
     "traceback": [
      "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m",
      "\u001b[1;31mKeyError\u001b[0m                                  Traceback (most recent call last)",
      "\u001b[1;32mc:\\Users\\lenovo\\miniconda3\\envs\\env_py38\\lib\\site-packages\\pandas\\core\\generic.py\u001b[0m in \u001b[0;36m?\u001b[1;34m(cls, axis)\u001b[0m\n\u001b[0;32m    546\u001b[0m             \u001b[1;32mreturn\u001b[0m \u001b[0mcls\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0m_AXIS_TO_AXIS_NUMBER\u001b[0m\u001b[1;33m[\u001b[0m\u001b[0maxis\u001b[0m\u001b[1;33m]\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m    547\u001b[0m         \u001b[1;32mexcept\u001b[0m \u001b[0mKeyError\u001b[0m\u001b[1;33m:\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m--> 548\u001b[1;33m             \u001b[1;32mraise\u001b[0m \u001b[0mValueError\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;34mf\"No axis named {axis} for object type {cls.__name__}\"\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m",
      "\u001b[1;31mKeyError\u001b[0m: 1",
      "\nDuring handling of the above exception, another exception occurred:\n",
      "\u001b[1;31mValueError\u001b[0m                                Traceback (most recent call last)",
      "\u001b[1;32m~\\AppData\\Local\\Temp\\ipykernel_28708\\1185182898.py\u001b[0m in \u001b[0;36m?\u001b[1;34m()\u001b[0m\n\u001b[1;32m----> 6\u001b[1;33m \u001b[1;31m# 关于sort_index函数\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m\u001b[0;32m      7\u001b[0m \u001b[0ms\u001b[0m \u001b[1;33m=\u001b[0m \u001b[0mpd\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mSeries\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;33m[\u001b[0m\u001b[1;36m1\u001b[0m\u001b[1;33m,\u001b[0m\u001b[1;36m2\u001b[0m\u001b[1;33m,\u001b[0m\u001b[1;36m3\u001b[0m\u001b[1;33m,\u001b[0m\u001b[1;36m4\u001b[0m\u001b[1;33m,\u001b[0m\u001b[1;36m5\u001b[0m\u001b[1;33m]\u001b[0m\u001b[1;33m,\u001b[0m\u001b[0mindex\u001b[0m\u001b[1;33m=\u001b[0m\u001b[1;33m[\u001b[0m\u001b[1;34m'A'\u001b[0m\u001b[1;33m,\u001b[0m\u001b[1;34m'b'\u001b[0m\u001b[1;33m,\u001b[0m\u001b[1;34m'c'\u001b[0m\u001b[1;33m,\u001b[0m\u001b[1;34m'd'\u001b[0m\u001b[1;33m,\u001b[0m\u001b[1;34m'E'\u001b[0m\u001b[1;33m]\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m      8\u001b[0m \u001b[1;31m# s.sort_index(axis=0,ascending=False,inplace=True,key=lambda x:x.str.lower())\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m      9\u001b[0m \u001b[1;33m\u001b[0m\u001b[0m\n",
      "\u001b[1;32mc:\\Users\\lenovo\\miniconda3\\envs\\env_py38\\lib\\site-packages\\pandas\\util\\_decorators.py\u001b[0m in \u001b[0;36m?\u001b[1;34m(*args, **kwargs)\u001b[0m\n\u001b[0;32m    307\u001b[0m                     \u001b[0mmsg\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mformat\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0marguments\u001b[0m\u001b[1;33m=\u001b[0m\u001b[0marguments\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m,\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m    308\u001b[0m                     \u001b[0mFutureWarning\u001b[0m\u001b[1;33m,\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m    309\u001b[0m                     \u001b[0mstacklevel\u001b[0m\u001b[1;33m=\u001b[0m\u001b[0mstacklevel\u001b[0m\u001b[1;33m,\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m    310\u001b[0m                 )\n\u001b[1;32m--> 311\u001b[1;33m             \u001b[1;32mreturn\u001b[0m \u001b[0mfunc\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;33m*\u001b[0m\u001b[0margs\u001b[0m\u001b[1;33m,\u001b[0m \u001b[1;33m**\u001b[0m\u001b[0mkwargs\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m",
      "\u001b[1;32mc:\\Users\\lenovo\\miniconda3\\envs\\env_py38\\lib\\site-packages\\pandas\\core\\series.py\u001b[0m in \u001b[0;36m?\u001b[1;34m(self, axis, level, ascending, inplace, kind, na_position, sort_remaining, ignore_index, key)\u001b[0m\n\u001b[0;32m   3613\u001b[0m         \u001b[0md\u001b[0m    \u001b[1;36m4\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m   3614\u001b[0m         \u001b[0mdtype\u001b[0m\u001b[1;33m:\u001b[0m \u001b[0mint64\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m   3615\u001b[0m         \"\"\"\n\u001b[0;32m   3616\u001b[0m \u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m-> 3617\u001b[1;33m         return super().sort_index(\n\u001b[0m\u001b[0;32m   3618\u001b[0m             \u001b[0maxis\u001b[0m\u001b[1;33m,\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m   3619\u001b[0m             \u001b[0mlevel\u001b[0m\u001b[1;33m,\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m   3620\u001b[0m             \u001b[0mascending\u001b[0m\u001b[1;33m,\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n",
      "\u001b[1;32mc:\\Users\\lenovo\\miniconda3\\envs\\env_py38\\lib\\site-packages\\pandas\\core\\generic.py\u001b[0m in \u001b[0;36m?\u001b[1;34m(self, axis, level, ascending, inplace, kind, na_position, sort_remaining, ignore_index, key)\u001b[0m\n\u001b[0;32m   4535\u001b[0m         \u001b[0mkey\u001b[0m\u001b[1;33m:\u001b[0m \u001b[0mIndexKeyFunc\u001b[0m \u001b[1;33m=\u001b[0m \u001b[1;32mNone\u001b[0m\u001b[1;33m,\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m   4536\u001b[0m     ):\n\u001b[0;32m   4537\u001b[0m \u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m   4538\u001b[0m         \u001b[0minplace\u001b[0m \u001b[1;33m=\u001b[0m \u001b[0mvalidate_bool_kwarg\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0minplace\u001b[0m\u001b[1;33m,\u001b[0m \u001b[1;34m\"inplace\"\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m-> 4539\u001b[1;33m         \u001b[0maxis\u001b[0m \u001b[1;33m=\u001b[0m \u001b[0mself\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0m_get_axis_number\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0maxis\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m\u001b[0;32m   4540\u001b[0m         \u001b[0mascending\u001b[0m \u001b[1;33m=\u001b[0m \u001b[0mvalidate_ascending\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mascending\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m   4541\u001b[0m \u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m   4542\u001b[0m         \u001b[0mtarget\u001b[0m \u001b[1;33m=\u001b[0m \u001b[0mself\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0m_get_axis\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0maxis\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n",
      "\u001b[1;32mc:\\Users\\lenovo\\miniconda3\\envs\\env_py38\\lib\\site-packages\\pandas\\core\\generic.py\u001b[0m in \u001b[0;36m?\u001b[1;34m(cls, axis)\u001b[0m\n\u001b[0;32m    544\u001b[0m     \u001b[1;32mdef\u001b[0m \u001b[0m_get_axis_number\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mcls\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0maxis\u001b[0m\u001b[1;33m:\u001b[0m \u001b[0mAxis\u001b[0m\u001b[1;33m)\u001b[0m \u001b[1;33m->\u001b[0m \u001b[0mint\u001b[0m\u001b[1;33m:\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m    545\u001b[0m         \u001b[1;32mtry\u001b[0m\u001b[1;33m:\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m    546\u001b[0m             \u001b[1;32mreturn\u001b[0m \u001b[0mcls\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0m_AXIS_TO_AXIS_NUMBER\u001b[0m\u001b[1;33m[\u001b[0m\u001b[0maxis\u001b[0m\u001b[1;33m]\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m    547\u001b[0m         \u001b[1;32mexcept\u001b[0m \u001b[0mKeyError\u001b[0m\u001b[1;33m:\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m--> 548\u001b[1;33m             \u001b[1;32mraise\u001b[0m \u001b[0mValueError\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;34mf\"No axis named {axis} for object type {cls.__name__}\"\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m",
      "\u001b[1;31mValueError\u001b[0m: No axis named 1 for object type Series"
     ]
    }
   ],
   "source": [
    "# 关于sort_index函数\n",
    "s = pd.Series([1,2,3,4,5],index=['A','b','c','d','E'])\n",
    "s.sort_index(axis=1,ascending=False,inplace=True,key=lambda x:x.str.lower())\n",
    "# s.sort_index(axis=0,ascending=False,inplace=True,key=lambda x:x.str.lower())\n",
    "print(s)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 43,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "生成随机数据元素： \n",
      "     col_0   col_1   col_2\n",
      "0  Otx7E4  02PaEY  tJqW3P\n",
      "6  SlKL72  gH9hJs  fnbKuK\n",
      "8  EEDHtP  UQ3HCh  35BBdp\n",
      "4  NtwVaC  hXy6TI  HZ4i4a\n",
      "9  jsPN7u  4k6EPE  26OYWa\n",
      "\n",
      "--------------------------------\n",
      "     col_0   col_1   col_2\n",
      "0  Otx7E4  02PaEY  tJqW3P\n",
      "4  NtwVaC  hXy6TI  HZ4i4a\n",
      "6  SlKL72  gH9hJs  fnbKuK\n",
      "8  EEDHtP  UQ3HCh  35BBdp\n",
      "9  jsPN7u  4k6EPE  26OYWa\n"
     ]
    }
   ],
   "source": [
    "# 生成随机元素\n",
    "import random\n",
    "import string\n",
    "def generate_random_str(length):\n",
    "    return ''.join(random.choices(string.ascii_letters + string.digits,k=length))\n",
    "\n",
    "def columns_rename(col):\n",
    "    return \n",
    "# 生成随机元素\n",
    "num_rows = 5\n",
    "num_cols = 3\n",
    "data = {f'col_{i}':[generate_random_str(6)\n",
    "                    for _ in range(num_rows) ] \n",
    "            for i in range(num_cols)}\n",
    "\n",
    "#创建DataFrame\n",
    "df = pd.DataFrame(data,index=[random.sample(string.digits,num_rows)])\n",
    "df2 = df.copy()\n",
    "df2.sort_index(axis=0,inplace=True,ascending=True)\n",
    "print('生成随机数据元素：','\\n',df)\n",
    "print(END_STRING,df2)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 44,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "        0       1       2\n",
      "0  Otx7E4  02PaEY  tJqW3P\n",
      "4  NtwVaC  hXy6TI  HZ4i4a\n",
      "6  SlKL72  gH9hJs  fnbKuK\n",
      "8  EEDHtP  UQ3HCh  35BBdp\n",
      "9  jsPN7u  4k6EPE  26OYWa\n"
     ]
    }
   ],
   "source": [
    "df2.rename(mapper=lambda f:f.split('_')[1],inplace=True,\n",
    "           axis='columns',copy=True)\n",
    "print(df2)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "                   Name        Born        Died  Age    Occupation    born_dt  \\\n",
      "0     Rosaline Franklin  1920-07-25  1958-04-16   37       Chemist 1920-07-25   \n",
      "1        William Gosset  1876-06-13  1937-10-16   61  Statistician 1876-06-13   \n",
      "2  Florence Nightingale  1820-05-12  1910-08-13   90         Nurse 1820-05-12   \n",
      "3           Marie Curie  1867-11-07  1934-07-04   66       Chemist 1867-11-07   \n",
      "4         Rachel Carson  1907-05-27  1964-04-14   56     Biologist 1907-05-27   \n",
      "\n",
      "     died_dt  \n",
      "0 1958-04-16  \n",
      "1 1937-10-16  \n",
      "2 1910-08-13  \n",
      "3 1934-07-04  \n",
      "4 1964-04-14  \n"
     ]
    }
   ],
   "source": [
    "born_datatime = pd.to_datetime(scientists['Born'],format = '%Y-%m-%d')\n",
    "died_datetime = pd.to_datetime(scientists['Died'],format = '%Y-%m-%d')\n",
    "scientists['born_dt'],scientists['died_dt'] = (born_datatime,died_datetime)\n",
    "print(scientists.head()) "
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "env_py38",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.8.18"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
